package com.lwx.sys.controller;


import com.lwx.sys.domain.Role;
import com.lwx.sys.domain.User;
import com.lwx.sys.service.PermissionService;
import com.lwx.sys.service.RoleService;
import com.lwx.sys.utils.DataGridView;
import com.lwx.sys.utils.ResultObj;
import com.lwx.sys.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 角色管理
 * @author lwx
 * @since 2020-04-14
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;

    @GetMapping("loadAllRole")
    public DataGridView loadAllRole(RoleVo roleVo){
        return roleService.loadAllRole(roleVo);
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @DeleteMapping("deleteRole")
    public ResultObj deleteRole(Integer id){
        try {
            roleService.deleteRole(id);
            return ResultObj.DELETE_SUCESS;
        }catch (Exception e){
            return ResultObj.DELETE_ERROR;
        }
    }

    //批量删除

    @DeleteMapping("/batchDeleteRole")
    public ResultObj batchDeleteRole(Integer[] ids){
        try {
            roleService.batchDeleteRole(ids);
            return ResultObj.DELETE_SUCESS;
        }catch (Exception e){
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @PostMapping("/addRole")
    public ResultObj addRole(Role role){
        try {
            role.setCreatetime(new Date());
            roleService.addRole(role);
            return ResultObj.ADD_SUCESS;
        }catch (Exception e){
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 更新角色
     * @param role
     * @return
     */
    @PostMapping("/updateRole")
    public ResultObj updateRole(Role role){
        try {
            roleService.updateRole(role);
            return ResultObj.ADD_SUCESS;
        }catch (Exception e){
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 根据id查找
     * @param id
     * @return
     */
    @GetMapping("/loadRoleById")
    public DataGridView loadRoleById(Integer id){
        return new DataGridView(roleService.getById(id));
    }

    /**
     * 加载角色所拥有的权限
     * @param roleId
     * @return
     */
    @GetMapping("/loadRolePermission")
    public DataGridView loadRolePermission(@RequestParam("id") Integer roleId){
        return permissionService.queryRolePermissionByRoleId(roleId);
    }

    /**
     * 保存角色所拥有的权限
     * @param roleId
     * @param pids
     * @return
     */
    @PostMapping("/saveRolePermission")
    public ResultObj saveRolePermission(Integer roleId,Integer[] pids){
        try{
            roleService.saveRolePermission(roleId,pids);
            return ResultObj.ADD_SUCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 根据用户id加载角色
     * @param userId
     * @return
     */
    @GetMapping("/loadRolesByUserId")
    public DataGridView loadRolesByUserId(Integer userId){
        return roleService.queryRolesByUserId(userId);
    }

}

